<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
    	$map['create_time'] = array('between', array(date("Y-m-d")." 00:00:00", date("Y-m-d")." 23:59:59"));
    	$today_user = M("User")->where($map)->count();
    	$today_wechat = M("Wechat")->where($map)->count();
    	$all_wechat = M("Wechat")->count();
    	$this->assign('today_user',$today_user);
    	$this->assign('today_wechat',$today_wechat);
    	$this->assign('all_wechat',$all_wechat);

     	$this->display();   
    }

    //代码审核结果
    public function audit_result()
    {
        $res = M('audit')->alias('a')->field('a.*,w.nick_name,w.pian')->join('tp_wechat w on w.wid = a.wid')->order('status asc')->select();
        $this->assign('list',$res);
        $this->display();

    }
    public function edit(){
        $model = M("AdminUser");
        $map['uid'] = 1;

        if(IS_AJAX){
            $new['username'] = I('post.username');
            if(empty($new['username'])){
                $this->error('请输入用户名');
            }
            $new['password'] = I('post.password');
            if(!empty($new['password'])){
                $new['password'] = md5($new['password']);
            }
            $model->where($map)->save($new);
            session(null);
            $this->success('修改成功，请重新登录！');
        }else{
            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);
            $this->display();
        }
    }
    //更新模板库
    public function template()
    {
        $authorizer_access_token = $this->get_component_access_token();
        //$res = $this->ihttp_get("https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token={$authorizer_access_token}");
        $res = $this->ihttp_get("https://api.weixin.qq.com/wxa/gettemplatelist?access_token={$authorizer_access_token}");
        $td = json_decode($res, true);
        if($td['errcode'] ===0){
        //更新成功
            $list = $td['template_list'];
            foreach($list as $k=>$v){
                $list[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
            $count = M('template')->count();
            if($count>0){
                $r = M('template')->where('1')->delete();
            }else{
                $r = 1;
            }
            if($r){
                $a = M('template')->addAll($list);
            }
            if($a){
                $this->success("更新成功");
            }else{
                $this->error("网络忙");
            }
        }else{
            $this->error($td['errmsg']);
        }
    }

    //提交审核单个小程序
    public function shen_one()
    {
        $wid =  I('id');
        //撤回审核小程序
        /*$map['wid'] = $wid;
        $model = M("Wechat");
        $info = $model->where($map)->find();
        $authorizer_access_token = $this->getAccessToken($info);
        $url = "https://api.weixin.qq.com/wxa/undocodeaudit?access_token={$authorizer_access_token}";
        $res = $this->ihttp_get($url);
        $td =json_decode($res,true);
        var_dump($td);exit;*/
        $this->fabu($wid);
    }
    //审核全部
    public function auditall()
    {
        $wechat = M('wechat')->where(['status'=>1])->select();
        foreach($wechat as $k=>$v){
            if($v['wid']){
                $re = $this->fabuall($v['wid']);
                if(!$re){
                    continue;
                }
            }
        }
        $this->success('提交完成');
    }

    //查询最新一次提交的审核状态
    // https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=TOKEN
    public function get_latest_auditstatus($wid)
    {
        $map['wid'] = $wid;
        $model = M("Wechat");
        $info = $model->where($map)->find();

        $audit = M('audit')->where(['wid'=>$wid])->find();
        if(!$audit){
            return $td['errcode'] =0;
            exit;
        }
        $authorizer_access_token= $this->getAccessToken($info);
        $url = "https://api.weixin.qq.com/wxa/get_auditstatus?access_token={$authorizer_access_token}";
        $post_data = array(
            "auditid"=>$audit['auditid'],
            );
        $json = json_encode($post_data, JSON_UNESCAPED_UNICODE);
        $res = $this->ihttp_post($url, $json);
        $td = json_decode($res, true);
        return $td;
    }

    //骗审
    public function pian(){
        $status = I('status');
        $new['pian']= $status;
        $a= M('wechat')->where(['wid'=>I('wid')])->save($new);
        if($a){
            $this->success("修改成功");
        }else{
            $this->success("网络繁忙，请稍后再试~");
        }
    }

    public function fabu($wid){
        $map['wid'] = $wid;
        $last = $this->get_latest_auditstatus($wid);//上次审核结果
        if($last['errcode']===0 && (I('type')!=1)){
            //审核状态，其中0为审核成功，1为审核失败，2为审核中
            if($last['status'] ==2){
                $data['status'] =$last['status'];
                M('audit')->where(['wid'=>$wid])->save($data);
                $this->error('审核中，请耐心等待');
            }
            if($last['status'] === 1){
                $data['reason'] = $last['reason'];
                $data['status'] =$last['status'];
                M('audit')->where(['wid'=>$wid])->save($data);
                $this->error('审核失败，请在审核结果页面查看具体原因');
            }
            if($last['status'] ===0){
                $data['status'] =$last['status'];
                M('audit')->where(['wid'=>$wid])->save($data);
                $re = M('audit')->where(['wid'=>$wid])->find();
                if($re['is_fabu'] != 1){
                    $this->success("审核成功，快到审核结果页面发布小程序吧");
                }
            }
        }
        if($last['errcode'] !=0){
            if($last['errcode'] == 86000){
                $msg = '不是由第三方代小程序进行调用';
            }
            if($last['errcode'] == 86001){
                $msg = '不存在第三方的已经提交的代码';
            }
            if($last['errcode'] == 85012){
                $msg = '无效的审核id';
            }
            $this->error($msg);
            exit;
        }
        // 重新提交审核
        if(I('type')==1){
            M('audit')->where(['wid'=>$wid])->delete();
        }
        $model = M("Wechat");
        $info = $model->where($map)->find();
        $token = $this->get_component_access_token();
        $authorizer_access_token = $this->getAccessToken($info);

        $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token={$authorizer_access_token}";
        $post_data = array(
            "action"=>'set',
            "requestdomain"=>array("https://www.58zltc.com",'https://apis.map.qq.com','https://tls.qcloud.com','https://webim.tim.qq.com'),
            "wsrequestdomain"=>array("wss://socket.58zltc.com"),
            "uploaddomain"=>array("https://www.58zltc.com"),
            "downloaddomain"=>array("https://www.58zltc.com"));
        $json = json_encode($post_data, JSON_UNESCAPED_UNICODE);
        $res = $this->ihttp_post($url, $json);
        $td = json_decode($res, true);
        if($td['errcode'] !== 0){
            $this->error($td['errmsg']);
        }
        if($td['errmsg'] == 'ok'){
            //设置业务域名
            $url2="https://api.weixin.qq.com/wxa/setwebviewdomain?access_token={$authorizer_access_token}";
            $post_data2 = array(
                "action"=>'get',
                //'webviewdomain'=>["https://www.58zltc.com",'https://720.58zltc.com']
            );
            $json2 = json_encode($post_data2, JSON_UNESCAPED_UNICODE);
            $res2 = $this->ihttp_post($url2, $json2);
            $td2 = json_decode($res2, true);
            //不是空的则更新
            if(count($td2['webviewdomain'])<2){
                $url3="https://api.weixin.qq.com/wxa/setwebviewdomain?access_token={$authorizer_access_token}";
                $post_data3 = array(
                    "action"=>'set',
                    'webviewdomain'=>["https://www.58zltc.com",'https://720.58zltc.com']
                );
                $json3 = json_encode($post_data3, JSON_UNESCAPED_UNICODE);
                $res3 = $this->ihttp_post($url3, $json3);
                $td3 = json_decode($res3, true);
            }

            $this->step2($wid);
        }
    }
    private function reAuth(){
        $appid = C("OPEN_APPID");
        $pre_auth_code = A("Home/Index")->get_pre_auth_code();

        // $pre_auth_code = $pre_auth_code."KG5F";
        $redirect_uri = urlencode("https://www.58zltc.com?type=reset");
        $url = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid={$appid}&pre_auth_code={$pre_auth_code}&redirect_uri={$redirect_uri}";
        $this->error('没有相应的权限，请重新授权！', $url);
    }
    public function step2($wid){
        $model = M("Wechat");
        $map['wid'] = $wid;
        $info = $model->where($map)->find();
        if($info['shop_id']){
            $info2 =M("shop")->where(['shop_id'=>$info['shop_id']])->find();
            switch($info2['type']){
                case " ":
                    $template_id = 409;
                    $user_desc = "通用";
                    break;
                case "":
                    $template_id = 409;
                    $user_desc = "通用";
                    break;
                case "jiaoyu":
                    $template_id =189;
                    $user_desc = "教育";
                    break;
                case "car":
                    $template_id = 188;
                    $user_desc = "汽车";
                    break;
                case "jiancai":
                    $template_id =169;
                    $user_desc = "建材";
                    break;
            }
        }else{
            $r = M('template')->order('id desc')->find();
            if($r){
                $template_id = $r['template_id'];
            }else{
                $this->error('请更新代码模板库');
                exit;
            }


            $user_desc ='测试';
        }
        //为授权的小程序帐号上传小程序代码 文档https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1489140610_Uavc4&token=&lang=zh_CN
        $authorizer_access_token = $this->getAccessToken($info);
        $url = "https://api.weixin.qq.com/wxa/commit?access_token={$authorizer_access_token}";
        $ext = array(
            "extEnable" => true,
            'ext'=>array(
                "host" => "https://www.58zltc.com",
                'appid'=>$info['appid'],
            ),
            'window'=>array(
                'navigationBarTitleText'=>$info['nick_name']
            )
        );
        $ext_json = json_encode($ext, JSON_UNESCAPED_UNICODE);
        $post_data = array(
            "template_id"=>$template_id,
            "ext_json"=>$ext_json,
            "user_version"=>'1.03',
            "user_desc"=>$user_desc);

        $json = json_encode($post_data, JSON_UNESCAPED_UNICODE);
        $res = $this->ihttp_post($url, $json);
        $td = json_decode($res, true);
        if($td['errcode'] !== 0){
            $this->error($td['errmsg']);
        }
        if($td['errcode'] ===0){
            $this->step3($wid,$template_id);
        }
    }
//查询最新一次提交的审核状态
    // https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=TOKEN
    public function get_latest_auditstatus2($wid)
    {
        $map['wid'] = $wid;
        $model = M("Wechat");
        $info = $model->where($map)->find();

        $audit = M('audit')->where(['wid'=>$wid])->find();
        if(!$audit){
            return $td['errcode'] =0;
            exit;
        }
        $authorizer_access_token= $this->getAccessToken2($info);
        $url = "https://api.weixin.qq.com/wxa/get_auditstatus?access_token={$authorizer_access_token}";
        $post_data = array(
            "auditid"=>$audit['auditid'],
        );
        $json = json_encode($post_data, JSON_UNESCAPED_UNICODE);
        $res = $this->ihttp_post($url, $json);
        $td = json_decode($res, true);
        return $td;
    }
    public function step3($wid,$template_id){
            $map['wid'] = $wid;
            $model = M("Wechat");
            $info = $model->where($map)->find();
            $shop = M("shop")->where(['shop_id'=>$info['shop_id']])->find();
            switch($shop['type']){
                case " ":
                    $pages = "pages/shop/detail";
                    $title = "通用";
                    break;
                case "":
                    $pages = "pages/shop/detail";
                    $title = "通用";
                    break;
                case "jiaoyu":
                    $pages = "pages/jiaoyu/detail";
                    $title = "教育";
                    break;
                case "car":
                    $pages = "pages/car/detail";
                    $title = "汽车";
                    break;
                case "jiancai":
                    $pages = "pages/jiancai/detail";
                    $title = "建材";
                    break;
            }
            //获取授权小程序帐号的可选类目
            $authorizer_access_token = $this->getAccessToken($info);
            $url = "https://api.weixin.qq.com/wxa/get_category?access_token={$authorizer_access_token}";
            $res = $this->ihttp_get($url);
            $td = json_decode($res, true);

            // dump($td);
            if($td['errcode'] !== 0){
                $this->error($td['errmsg']);
            }
            if(count($td['category_list']) == 0){
                $this->error('请登录微信官方小程序后台设置服务类目！');
            }
            $cate = $td['category_list'][0];

            //将第三方提交的代码包提交审核 https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1489140610_Uavc4&token=&lang=zh_CN
            $url = "https://api.weixin.qq.com/wxa/submit_audit?access_token={$authorizer_access_token}";

            $post_data = array(
                "item_list"=>array(
                    array(
                        "address"=>$pages?$pages:"pages/index/index",
                        "tag"=>"生活,同城",
                        "first_class"=>$cate['first_class'],
                        "second_class"=>$cate['second_class'],
                        "first_id"=>$cate['first_id'],
                        "second_id"=>$cate['second_id'],
                        "title"=>$title?$title:"首页",
                    )
                )
            );
            $json = json_encode($post_data, JSON_UNESCAPED_UNICODE);

            // dump($json);
            // exit();
            $res = $this->ihttp_post($url, $json);
            $td = json_decode($res, true);
            if($td['errcode'] !== 0){
                switch ($td['errcode']){
                    case 86000:
                        $msg='不是由第三方代小程序进行调用';
                        break;
                    case 86001:
                        $msg='不存在第三方的已经提交的代码';
                        break;
                    case 85009:
                        $msg='已经有正在审核的版本';
                        break;
                    case 85085:
                        $msg='近7天提交审核的小程序数量过多，请耐心等待审核完毕后再次提交';
                        break;
                    case 85086:
                        $msg='提交代码审核之前需提前上传代码';
                        break;
                    default:
                        $msg='上传失败';
                        break;
                }
                $this->error($msg);
            }
            else{
                $re = M('audit')->where(['wid'=>$wid])->find();
                if($re){
                    M('audit')->where(['wid'=>$wid])->delete();
                }
                $data['wid'] = $wid;
                $data['template_id'] =$template_id;
                $data['auditid'] = $td['auditid'];
                $data['time'] = date('Y-m-d H:i:s',time());
                $data['status'] = 2;//审核状态，其中0为审核成功，1为审核失败，2为审核中
                $data['is_fabu'] =0;
                M('audit')->add($data);
                $this->success("提交审核成功，请耐心等待吧");
            }
            //$this->redirect("step3");

    }
    public function fabuall($wid){
        $map['wid'] = $wid;
        $last = $this->get_latest_auditstatus2($wid);//上次审核结果
        if($last['errcode']===0 ){
            //审核状态，其中0为审核成功，1为审核失败，2为审核中
            if($last['status'] ==2){
                $data['status'] =$last['status'];
                M('audit')->where(['wid'=>$wid])->save($data);
            }
            if($last['status'] === 1){
                $data['reason'] = $last['reason'];
                $data['status'] =$last['status'];
                M('audit')->where(['wid'=>$wid])->save($data);
            }
            if($last['status'] ===0){
                $data['status'] =$last['status'];
                M('audit')->where(['wid'=>$wid])->save($data);
            }
            return false;
        }
        if($last['errcode'] !=0){
            return false;
        }

        $model = M("Wechat");
        $info = $model->where($map)->find();
        $token = $this->get_component_access_token2();
        $authorizer_access_token = $this->getAccessToken2($info);

        $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token={$authorizer_access_token}";
        $post_data = array(
            "action"=>'set',
            "requestdomain"=>array("https://www.58zltc.com",'https://apis.map.qq.com','https://tls.qcloud.com','https://webim.tim.qq.com'),
            "wsrequestdomain"=>array("wss://socket.58zltc.com"),
            "uploaddomain"=>array("https://www.58zltc.com"),
            "downloaddomain"=>array("https://www.58zltc.com"));
        $json = json_encode($post_data, JSON_UNESCAPED_UNICODE);
        $res = $this->ihttp_post($url, $json);
        $td = json_decode($res, true);
        if($td['errcode'] !== 0){
            return false;
        }
        if($td['errmsg'] == 'ok'){
            //设置业务域名
            $url2="https://api.weixin.qq.com/wxa/setwebviewdomain?access_token={$authorizer_access_token}";
            $post_data2 = array(
                "action"=>'get',
                //'webviewdomain'=>["https://www.58zltc.com",'https://720.58zltc.com']
            );
            $json2 = json_encode($post_data2, JSON_UNESCAPED_UNICODE);
            $res2 = $this->ihttp_post($url2, $json2);
            $td2 = json_decode($res2, true);
            //不是空的则更新
            if(count($td2['webviewdomain'])<2){
                $url3="https://api.weixin.qq.com/wxa/setwebviewdomain?access_token={$authorizer_access_token}";
                $post_data3 = array(
                    "action"=>'set',
                    'webviewdomain'=>["https://www.58zltc.com",'https://720.58zltc.com']
                );
                $json3 = json_encode($post_data3, JSON_UNESCAPED_UNICODE);
                $res3 = $this->ihttp_post($url3, $json3);
                $td3 = json_decode($res3, true);
            }

            $this->stepall2($wid);
        }
    }

    public function stepall2($wid){
        $model = M("Wechat");
        $map['wid'] = $wid;
        $info = $model->where($map)->find();
        if($info['shop_id']){
            $info2 =M("shop")->where(['shop_id'=>$info['shop_id']])->find();
            switch($info2['type']){
                case " ":
                    $template_id = 409;
                    $user_desc = "通用";
                    break;
                case "":
                    $template_id = 409;
                    $user_desc = "通用";
                    break;
                case "jiaoyu":
                    $template_id =189;
                    $user_desc = "教育";
                    break;
                case "car":
                    $template_id = 188;
                    $user_desc = "汽车";
                    break;
                case "jiancai":
                    $template_id =169;
                    $user_desc = "建材";
                    break;
            }
        }else{
            $r = M('template')->order('id desc')->find();
            if($r){
                $template_id = $r['template_id'];
            }else{
                return false;
            }
            $user_desc ='测试';
        }
        //为授权的小程序帐号上传小程序代码 文档https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1489140610_Uavc4&token=&lang=zh_CN
        $authorizer_access_token = $this->getAccessToken2($info);
        $url = "https://api.weixin.qq.com/wxa/commit?access_token={$authorizer_access_token}";
        $ext = array(
            "extEnable" => true,
            'ext'=>array(
                "host" => "https://www.58zltc.com",
                'appid'=>$info['appid'],
            ),
            'window'=>array(
                'navigationBarTitleText'=>$info['nick_name']
            )
        );
        $ext_json = json_encode($ext, JSON_UNESCAPED_UNICODE);
        $post_data = array(
            "template_id"=>$template_id,
            "ext_json"=>$ext_json,
            "user_version"=>'1.03',
            "user_desc"=>$user_desc);
        $json = json_encode($post_data, JSON_UNESCAPED_UNICODE);
        $res = $this->ihttp_post($url, $json);
        $td = json_decode($res, true);
        if($td['errcode'] !== 0){
            return false;
        }
        if($td['errcode'] ===0){
            $this->stepall3($wid,$template_id);
        }
    }
    public function stepall3($wid,$template_id){
        $map['wid'] = $wid;
        $model = M("Wechat");
        $info = $model->where($map)->find();
        $shop = M("shop")->where(['shop_id'=>$info['shop_id']])->find();
        switch($shop['type']){
            case " ":
                $pages = "pages/shop/detail";
                $title = "通用";
                break;
            case "":
                $pages = "pages/shop/detail";
                $title = "通用";
                break;
            case "jiaoyu":
                $pages = "pages/jiaoyu/detail";
                $title = "教育";
                break;
            case "car":
                $pages = "pages/car/detail";
                $title = "汽车";
                break;
            case "jiancai":
                $pages = "pages/jiancai/detail";
                $title = "建材";
                break;
        }
        //获取授权小程序帐号的可选类目
        $authorizer_access_token = $this->getAccessToken2($info);
        $url = "https://api.weixin.qq.com/wxa/get_category?access_token={$authorizer_access_token}";
        $res = $this->ihttp_get($url);
        $td = json_decode($res, true);

        // dump($td);
        if($td['errcode'] !== 0){
            return false;
        }
        if(count($td['category_list']) == 0){
            return false;
            $this->error('请登录微信官方小程序后台设置服务类目！');
        }
        $cate = $td['category_list'][0];
        //将第三方提交的代码包提交审核 https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1489140610_Uavc4&token=&lang=zh_CN
        $url = "https://api.weixin.qq.com/wxa/submit_audit?access_token={$authorizer_access_token}";

        $post_data = array(
            "item_list"=>array(
                array(
                    "address"=>$pages?$pages:"pages/index/index",
                    "tag"=>"生活,同城",
                    "first_class"=>$cate['first_class'],
                    "second_class"=>$cate['second_class'],
                    "first_id"=>$cate['first_id'],
                    "second_id"=>$cate['second_id'],
                    "title"=>$title?$title:"首页",
                )
            )
        );
        $json = json_encode($post_data, JSON_UNESCAPED_UNICODE);
        $res = $this->ihttp_post($url, $json);
        $td = json_decode($res, true);
        if($td['errcode'] !== 0){
            return false;
        }
        else{
            $data['wid'] = $wid;
            $data['template_id'] =$template_id;
            $data['auditid'] = $td['auditid'];
            $data['time'] = date('Y-m-d H:i:s',time());
            $data['status'] = 2;//审核状态，其中0为审核成功，1为审核失败，2为审核中
            $data['is_fabu'] =0;
            M('audit')->add($data);
            //提交审核成功并开启骗审开关
            M('wechat')->where(['wid'=>$wid])->save(['pian'=>1]);
            return true;
        }
    }
    public function fabu2()
    {
        $wid = I('wid');
        $map['wid'] = $wid;
        $model = M("Wechat");
        $info = $model->where($map)->find();
        $authorizer_access_token = $this->getAccessToken($info);
        $res= $this->ihttp_post("https://api.weixin.qq.com/wxa/release?access_token={$authorizer_access_token}", "{}");
        $td = json_decode($res, true);
        if($td['errcode'] ==85019){
            $this->error('没有审核版本');
        }
        if($td['errcode'] ==85020){
            $this->error('审核状态未满足发布');
        }
        if($td['errcode'] === 0){
            M('audit')->where(['wid'=>$wid])->save(['is_fabu'=>1]);
            $this->success('发布成功');
        }
    }

    private function getAccessToken($info){
        if(date("Y-m-d H:i:s") < $info['token_end_time']){
            return $info['authorizer_access_token'];
        }else{
            $token = $this->get_component_access_token();
            $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token={$token}";
            $post_data = array(
                "component_appid"=>C("OPEN_APPID"),
                "authorizer_appid"=>$info['appid'],
                "authorizer_refresh_token"=>$info['authorizer_refresh_token']);
            $json = json_encode($post_data, JSON_UNESCAPED_UNICODE);
            $res = $this->ihttp_post($url, $json);
            $td = json_decode($res, true);
            if($td['authorizer_access_token']){
                $map['wid'] = $info['wid'];
                $data['authorizer_access_token'] = $td['authorizer_access_token'];
                $data['authorizer_refresh_token'] = $td['authorizer_refresh_token'];
                $data['token_end_time'] = date("Y-m-d H:i:s", time()+$td['expires_in']-60);
                M("Wechat")->where($map)->save($data);
                return $td['authorizer_access_token'];
            }else{
                $this->reAuth();
            }
        }
    }

    private function get_component_access_token(){
        $token = S("component_access_token");
        if($token){
            return $token;
        }
        $url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
        $post_data = array(
            "component_appid"=>C("OPEN_APPID"),
            "component_appsecret"=>C("OPEN_APPSECRET"),
            "component_verify_ticket"=>S("component_verify_ticket"));
        $json = json_encode($post_data, JSON_UNESCAPED_UNICODE);
        $res = $this->ihttp_post($url, $json);
        $td = json_decode($res, true);
        if($td['component_access_token']){
            S("component_access_token", $td['component_access_token'], $td['expires_in'] - 120);
            return $td['component_access_token'];
        }else{
            return false;
        }
    }
    private function getAccessToken2($info){
        if(date("Y-m-d H:i:s") < $info['token_end_time']){
            return $info['authorizer_access_token'];
        }else{
            $token = $this->get_component_access_token2();
            $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token={$token}";
            $post_data = array(
                "component_appid"=>C("OPEN_APPID"),
                "authorizer_appid"=>$info['appid'],
                "authorizer_refresh_token"=>$info['authorizer_refresh_token']);
            $json = json_encode($post_data, JSON_UNESCAPED_UNICODE);
            $res = $this->ihttp_post($url, $json);
            $td = json_decode($res, true);
            if($td['authorizer_access_token']){
                $map['wid'] = $info['wid'];
                $data['authorizer_access_token'] = $td['authorizer_access_token'];
                $data['authorizer_refresh_token'] = $td['authorizer_refresh_token'];
                $data['token_end_time'] = date("Y-m-d H:i:s", time()+$td['expires_in']-60);
                M("Wechat")->where($map)->save($data);
                return $td['authorizer_access_token'];
            }else{
                return false;
            }
        }
    }

    private function get_component_access_token2(){
        $token = S("component_access_token");
        if($token){
            return $token;
        }
        $url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
        $post_data = array(
            "component_appid"=>C("OPEN_APPID"),
            "component_appsecret"=>C("OPEN_APPSECRET"),
            "component_verify_ticket"=>S("component_verify_ticket"));
        $json = json_encode($post_data, JSON_UNESCAPED_UNICODE);
        $res = $this->ihttp_post($url, $json);
        $td = json_decode($res, true);
        if($td['component_access_token']){
            S("component_access_token", $td['component_access_token'], $td['expires_in'] - 120);
            return $td['component_access_token'];
        }else{
            return false;
        }
    }
    protected function ihttp_post($url, $post_data){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $out = curl_exec($ch);
        curl_close($ch);
        return $out;
    }

    protected function ihttp_get($url, $is_https = true){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if($is_https){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        $out = curl_exec($ch);
        curl_close($ch);
        return $out;
    }
}